// This page provides functionality to delete/edit the hospitals
// Created on : may 06,06
// Author : Rupinder Saini

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using SiteManager;

public partial class Admin_Ad_ManageFeaturedHospitals : Page
{
    // creating an instance of manager class
    Manager mgr = new Manager(true, "ConnectionString");
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMsg.Text = "";
        if (!Page.IsPostBack)
        {
            grdmedsch.Visible = true;
            BindGridView();
            BindFeaturedHospitals();
        }
        foreach (GridViewRow rv in grdmedsch.Rows)
        {
            CheckBox chkRow = (CheckBox)rv.FindControl("RowLevelCheckBox");
            chkRow.Attributes.Add("onclick", "ChangeHeaderAsNeeded();");
            ClientScript.RegisterArrayDeclaration("CheckBoxIDs", String.Concat("'", chkRow.ClientID, "'"));
        }

    }

    public void BindFeaturedHospitals()
    {

        var sqlparam = new SqlParameter[0];
        var ds = CustomRepository.CustumDataSetFunction("Get_InterNational_Hospitals", sqlparam);


        Featured_Hospital_Repeater.DataSource = ds.Tables[1];
        Featured_Hospital_Repeater.DataBind();

    
    }
    //paging
    protected void grdmedsch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdmedsch.PageIndex = e.NewPageIndex;
        BindGridView();
    }

    // delete confirmation
    protected void grdmedsch_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //---------------------------------------------------------------
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox chkBox = (CheckBox)e.Row.FindControl("HeaderLevelCheckBox");
            chkBox.Attributes.Add("onclick", "ChangeAllCheckBoxStates(this.checked);");
        }

        //---------------------------------------------------------------

    }

   
    #region general functions

    /// <summary>
    /// binds the grid view 
    /// </summary>

    public void BindGridView()
    {
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[5];

            sqlParam[0] = new SqlParameter("@condition", "featured");
            sqlParam[1] = new SqlParameter("@name", txtName.Text.Trim());
            sqlParam[2] = new SqlParameter("@address", "");
            sqlParam[3] = new SqlParameter("@country", "");
            sqlParam[4] = new SqlParameter("@zipcode", "");

            DataSet ds = mgr.ExecuteQuery("[dbo].[USP_Get_International_Hospitals]", sqlParam, "tbl_usa_hospitals");
            if (ds.Tables[0].Rows.Count > 0)
            {
                grdmedsch.DataSource = ds;
                grdmedsch.PageSize = Convert.ToInt32(Session["AdminPageSize"].ToString());
                grdmedsch.DataBind();
                lbltotHosp.Text = ds.Tables[0].Rows.Count.ToString();
                //added by ranjeet at 29/02/2008
                //        TotalSearchHospital();
            }
            else
            {

                lblMsg.Text = "No Records found!!<br>Please click the Add Hospital link on the left panel to add a new hospital.";
                lblMsg.ForeColor = System.Drawing.Color.Red;
                grdmedsch.Visible = false;
                lblMsg.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    #endregion

    protected void butSerach_Click(object sender, EventArgs e)
    {
        grdmedsch.Visible = true;
        BindGridView();
    }
    protected void grdmedsch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Featured")
        {
            foreach (GridViewRow rv in grdmedsch.Rows)
            {
                CheckBox chk = (CheckBox)rv.FindControl("RowLevelCheckBox");
                if (chk.Checked)
                {
                    int id = Convert.ToInt32(grdmedsch.DataKeys[rv.RowIndex].Value.ToString());
                    SqlParameter[] sqlParam = new SqlParameter[3];
                    sqlParam[0] = new SqlParameter("@condition", "featured");
                    sqlParam[1] = new SqlParameter("@ID", id);
                    sqlParam[2] = new SqlParameter("@Value", "1");
                    mgr.ExecuteUpdate("USP_UpdateHospitalStatus", sqlParam);
                }
            }
            BindGridView();
            BindFeaturedHospitals();
            //  lbltotHosp.Text = TotalHospital().ToString();
        }
    }

    protected void btndetele_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton delbutton = (sender as ImageButton);
        if (delbutton.CommandName == "Remove") ;
        {
            SqlParameter[] sqlParam = new SqlParameter[3];
            sqlParam[0] = new SqlParameter("@condition", "featured");
            sqlParam[1] = new SqlParameter("@ID", delbutton.CommandArgument.ToString());
            sqlParam[2] = new SqlParameter("@Value", "0");
            mgr.ExecuteUpdate("USP_UpdateHospitalStatus", sqlParam);
            //1- call your method with passing in delbutton.CommandArgument - it will give you key/ whatever you like
            //2- Rebind the Repeater here and that will bind controls again...
            BindGridView(); 
            BindFeaturedHospitals();
        }
    }
}
